from tkinter import *
import sound


class display:
    speed = 5  # 声速
    sound_type = 0  # 声音类型
    volume = 5  # 声音大小
    pit = 5  # 声音音调
    file_way = ""  # 文件路径
    file_name = ""  # 文件名称
    flag = -1  # 0为保存在指定目录 1为保存在当下目录
    be_read = ""  # 被读取的内容
    APP_ID = ""  # 登录信息
    API_KEY = ""
    SECRET_KEY = ""
    flag1 = -1  # 1为直接读取内容 0为读取文件内的内容

    def create_mainloop(self):
        window = Tk()
        window.title("主界面")
        window.geometry("1150x490")

        lb1 = Label(text="请输入你想要的语速：（0-10越来越快）", font=("仿宋", 18))  # 标签和对应的输入框
        lb1.place(x=30, y=30)
        en1 = Entry(window, highlightcolor='black')
        en1.place(x=30, y=60, height=35, width=200)

        lb2 = Label(text="请输入你想要的声音大小：（0-10越来越大）", font=("仿宋", 18))  # 标签和对应的输入框
        lb2.place(x=30, y=100)
        en2 = Entry(window, highlightcolor='yellow', relief="groove")
        en2.place(x=30, y=130, height=35, width=200)

        lb3 = Label(text="请输入你想要的声音类型：", font=("仿宋", 18))  # 标签和对应的输入框
        lb3.place(x=30, y=170)
        en3 = Entry(window, highlightcolor='yellow', relief="groove")
        en3.place(x=30, y=200, height=35, width=200)

        lb4 = Label(text="请输入你想要的音调：（0-10越来越高）", font=("仿宋", 18))  # 标签和对应的输入框
        lb4.place(x=30, y=240)
        en4 = Entry(window, highlightcolor='yellow', relief="groove")
        en4.place(x=30, y=270, height=35, width=200)

        lb6 = Label(text="请输入音频文件名称(示例：音频文件A)", font=("仿宋", 18))  # 标签和对应的输入框
        lb6.place(x=30, y=310)
        en6 = Entry(window, highlightcolor='yellow', relief="groove")
        en6.place(x=30, y=340, height=35, width=200)

        lb7 = Label(text="声音类型如下\n"  # 所有的声音类型
                         "普通机器女声A:0   普通机器男声A:1\n"
                         "普通机器男声B:3   机器萝莉声:4\n"
                         "机器机器女声B:5   可爱女生:103\n"
                         "充满智慧的男性:106 女小学生:110\n"
                         "甜美女生:111 还原度比较高的男声:5003\n"
                         " 还原度高的女声:5118\n"
                    , font=("黑体", 12))
        lb7.place(x=650, y=280)

        lb5 = Label(text = "请输入你想要将音频文件保存路径(示例：D:\python\music\)", font=("仿宋", 16))
        en5 = Entry(window, highlightcolor='yellow', relief="groove")  # 文件路径

        no = Radiobutton(window, text="保存音频到指定目录", value=0, variable=1,    # 点击是的话，调整flag为0 后续依据flag判断是保存在哪个目录中
                         command=lambda: [display.flag_set0(self, en5=en5, lb5=lb5)])
        yes = Radiobutton(window, text="保存音频到当下目录", value=1, variable=1,
                          command=lambda: [display.flag_set1(self, en5=en5, lb5=lb5)])

        yes.place(x=30, y=400)
        no.place(x=30, y=430)

        lb7 = Label(text="请输入你的APP_ID：", font=("仿宋", 13))
        lb7.place(x=600, y=30)
        en7 = Entry(window, highlightcolor='yellow')
        en7.place(x=770, y=30, width=250)

        lb8 = Label(text="请输入你的API_KEY：", font=("仿宋", 13))
        lb8.place(x=600, y=60)
        en8 = Entry(window)
        en8.place(x=770, y=60, width=250)

        lb9 = Label(text="请输入你的SECRET_KEY：", font=("仿宋", 13))
        lb9.place(x=600, y=90)
        en11 = Entry(window)
        en11.place(x=800, y=90, width=250)
        en9 = Entry(window)
        en9.place(x=720, y=160, width=350, height=40)
        bu5 = Button(window, text="确定啦！", height=1, width=10, font="黑体",  # 点击确定了后，将所有在输入框内的内容一并读取，然后存到对应的变量中
                     command=lambda: [display.button_set_pit(self, en4), display.button_set_type(self, en3),
                                      display.button_set_speed(self, en1), display.button_set_volume(self, en2),
                                      display.button_set_fileway(self, en5), display.button_set_filename(self, en6),
                                      display.read_inget(self, en9), display.register(self, en7, en8, en11),
                                      display.connect(self)])
        bu5.place(x=600, y=230)

        no1 = Radiobutton(window, text="导入转换内容", value=0, variable=2,
                          command=display.set_modle2(self))
        yes1 = Radiobutton(window, text="输入转换内容", value=1, variable=2, command=lambda: [display.set_modle1(self)])
        no1.place(x=600, y=180)
        yes1.place(x=600, y=150)
        window.mainloop()

    def set_modle1(self):
        self.flag1 = 1

    def set_modle2(self):
        self.flag1 = 0

    def read_inget(self, en9): # 依据flag写入be_read的内容
        if self.flag1 == 1:
            self.be_read = en9.get()
        else:
            self.be_read = open(en9.get(), "r+", encoding="UTF-8").read()
        print(self.be_read)

    def register(self, en7, en8, en11): # 一并写入所有需要登录的内容
        self.APP_ID = en7.get()
        self.API_KEY = en8.get()
        self.SECRET_KEY = en11.get()

    def flag_set1(self, lb5, en5): # 依据flag1判断路径
        self.flag = 1
        lb5.place_forget()
        en5.place_forget()

    def flag_set0(self, lb5, en5):
        self.flag = 0
        lb5.place(x=190, y=400)
        en5.place(x=200, y=430, height=35, width=200)

    def button_set_speed(self, w):  # 赋值方法
        self.speed = w.get()

    def button_set_type(self, w):
        self.sound_type = w.get()

    def button_set_volume(self, w):
        self.volume = w.get()

    def button_set_pit(self, w):
        self.pit = w.get()

    def button_set_fileway(self, en5):
        if self.flag == 0:
            self.file_way = en5.get()
        else:
            self.file_way = ""

    def button_set_filename(self, w):
        self.file_name = w.get()

    def connect(self):  # 与sound建立连接
        so = sound.sound(self.speed, self.sound_type, self.volume, self.pit, self.file_name, self.file_way,
                         self.be_read, self.APP_ID, self.API_KEY, self.SECRET_KEY)

    def __init__(self):
        print("可视化桌面的构造\n")
        display.create_mainloop(self)
